package com.sdtele.tta.operation;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.sdtele.tta.dac.Dac;
import com.sdtele.tta.models.JsonRequest;
import com.sdtele.tta.models.JsonResponse;
import com.sdtele.tta.models.Task;
import com.sdtele.tta.models.User;
import com.sdtele.tta.utils.TeamUtils;

/**
 * 操作类实现：GetUser(json.operation=getUser)
 * 
 * @author XCream
 */
public class OperationChgTask extends Operation {

	@Override
	public JsonResponse handle(JsonRequest jsonRequest) {
		JsonResponse jres = new JsonResponse();
		User curUser = (User) request.getSession().getAttribute("user");
		long taskId = jsonRequest.getJsonObject().get("taskId").getAsLong();
		Task task = Dac.getInstance().getTaskbyTaskid(taskId);

		if (!curUser.equals(null)) {
			if (curUser.getId() == task.getOwnerId()) {
				Long ownerId = jsonRequest.getJsonObject().get("ownerId").getAsLong();
				String title = jsonRequest.getJsonObject().get("title").getAsString();
				String description = jsonRequest.getJsonObject().get("description").getAsString();
				String deadLineStr = jsonRequest.getJsonObject().get("deadLine").getAsString();
				DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				java.util.Date ud = new java.util.Date();
				Date deadLine = new java.sql.Date(ud.getTime());
				try {
					deadLine = (Date) sdf.parse(deadLineStr);
				} catch (Exception e) {
					e.printStackTrace();
				}
				Long emergency = jsonRequest.getJsonObject().get("emergency").getAsLong();
				Integer completion = jsonRequest.getJsonObject().get("completion").getAsInt();
				Long state = jsonRequest.getJsonObject().get("state").getAsLong();
				String tags = jsonRequest.getJsonObject().get("tags").getAsString();				
				Timestamp updateTime = new Timestamp(System.currentTimeMillis());
				Long fatherTaskId = jsonRequest.getJsonObject().get("fatherTaskId").getAsLong();
				if (title.isEmpty()) {
					jres.setStatus(1);
					jres.setMessage("任务标题不允许为空");
					return jres;
				}
				if(!(ownerId.equals(null))) {
					task.setOwnerId(ownerId);
				}
				if(!(title.equals(null))) {
					task.setTitle(title);
				}
				if(!(description.equals(null))) {
					task.setDescription(description);
				}
				if(!(deadLine.equals(null))) {
					task.setDeadLine(deadLine);
				}
				if(!(emergency.equals(null))) {
					task.setEmergency(emergency);
				}
				if(!(completion.equals(null))) {
					task.setCompletion(completion);
				}
				if(!(state.equals(null))) {
					task.setState(state);
				}
				if(!(tags.equals(null))) {
					task.setTags(tags);
				}				
				task.setUpdateTime(updateTime);
				if(!(fatherTaskId.equals(null))) {
					task.setFatherTaskId(fatherTaskId);
				}				
				if (Dac.getInstance().chgTask(task)) {
					jres.setStatus(0);
					jres.setMessage("修改任务成功");
					jres.setEntity(task);
				} else {
					jres.setStatus(1);
					jres.setMessage("修改任务失败");
				}
			} else {
				jres.setStatus(1);
				jres.setMessage("您没有权限修改此任务");
			}
		} else {
			jres.setStatus(1);
			jres.setMessage("用户未登录");
		}
		return jres;
	}

}
